//
//  ProDetailTagCell.m
//  LazyCat
//
//  Created by Abner on 16/8/15.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "ProDetailTagCell.h"
#import "SFTagView.h"
#import "SFTag.h"
#import "AddProduct.h"

@interface ProDetailTagCell ()
/** 存储每个分区的数组[数组中放数组] */
@property(nonatomic,strong)NSMutableArray *sectionArrs;
/** 每个分区当前选中的索引的数组【数组放索引】 */
@property(nonatomic,strong)NSMutableArray *currentSelectedArrs;
@end

@implementation ProDetailTagCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self creatProDetailTagCellUI];
    }
    return self;
}

-(void)creatProDetailTagCellUI
{
    self.leftLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.leftLB];
    self.leftLB.font = FontSet(12);
    self.leftLB.textColor = SetColorRGBValue(0x878787, 1.0);
    
    self.tagView = [[SFTagView alloc] init];
    [self.contentView addSubview:self.tagView];
    // alignment 0 左对齐布局 1 右对齐布局
    self.tagView.alignment = 0;
    self.tagView.margin    = UIEdgeInsetsMake(9, 0, 9, 0); //整体
    self.tagView.insets    = 10; //左右间距
    self.tagView.lineSpace = 9; //上下间距
    
    self.bottomLine = [[UILabel alloc]init];
    self.bottomLine.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    [self.contentView addSubview:self.bottomLine];
    
    _sectionArrs = [NSMutableArray array];
    _currentSelectedArrs = [NSMutableArray array];

}

-(void)setupProDetailTagCellData:(NSString *)name indexRow:(NSInteger)indexRow andTags:(NSArray *)tags andSelectedIndex:(NSString *)selectedIndex
{
    NSInteger selectedIndexCount = [selectedIndex integerValue];
    self.leftLB.textColor = SetColorRGBValue(0x999999, 1.0);
    self.leftLB.font = FontSet(12);
    self.leftLB.text = name;
    CGFloat leftLBW = [self.leftLB.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 12) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontSet(13)} context:nil].size.width;
    
    self.leftLB.frame = CGRectMake(12, 0, leftLBW, 44);
    
    SFTagView *tagView = [[SFTagView alloc] initWithFrame:CGRectMake(self.leftLB.right, 0, ScreenWidth-self.leftLB.right-12, 44)];
    self.tagView = tagView;
    [self.contentView addSubview:tagView];
    
    // alignment 0 左对齐布局 1 右对齐布局
    tagView.alignment = 0;
    tagView.margin    = UIEdgeInsetsMake(9, 0, 9, 0); //整体
    tagView.insets    = 10; //左右间距
    tagView.lineSpace = 9; //上下间距
    for (int j = 0; j<tags.count; j++) {
        SFTag *tag = [SFTag tagWithText:tags[j]];
        tag.textColor = SetColorRGBValue(0x333333, 1.0);
        tag.bgColor = [UIColor whiteColor];
        tag.target = self;
        tag.action = @selector(handleBtn:);
        tag.leftRightMargin = 12;
        tag.topBottomMargin = 4;
        tag.cornerRadius = 12;
        tag.borderWidth = 1;
        tag.font = FontSet(13);
        tag.borderColor = SetColorRGBValue(0xe0e0e0, 1.0);
        NSString *indexStr = [NSString stringWithFormat:@"%ld%d",indexRow+1,j];
        tag.tagIndex = [indexStr integerValue];
        [tagView addTag:tag];
        //            NSLog(@"tagView frame: %@", NSStringFromCGRect(tagView.frame));
        
    }
    if (selectedIndexCount >= 0) {
        [tagView setupOneSelectedTag:selectedIndexCount];
    }
    //数组赋值。当_sectionArrs每添加一个tagView，_currentSelectedArrs也对应加一个@""；做到同步
    [_sectionArrs addObject:(SFTagView *)tagView];
    [_currentSelectedArrs addObject:@""];
    
    if (tagView.bottom < 44) {
        tagView.bottom = 44;
    }
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, tagView.bottom, ScreenWidth, 0.5)];
    self.bottomLine = line;
    line.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    [self.contentView addSubview:line];
    
}

-(void)handleBtn:(UIButton *)btn
{
    NSString *tagStr = [NSString stringWithFormat:@"%ld",(long)btn.tag];
    NSString *indexNum = [tagStr substringFromIndex:1];
    NSInteger indexCount = [indexNum integerValue];
    SFTagView *tagView = _sectionArrs[0]; //因为cell只有一个
   
    [_currentSelectedArrs replaceObjectAtIndex:0 withObject:indexNum];
    
    //告诉详情页，点击了那个标签
    if (self.ProDetailTagCellBlcok) {
         [tagView setupOneSelectedTag:indexCount];
        self.ProDetailTagCellBlcok(indexCount);
    }
    
}
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProDetailTagCellIdentifier";
    ProDetailTagCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProDetailTagCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


//*************************品牌*****************************//
@implementation ProDetailBrandCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self creatProDetailBrandCellUI];
    }
    return self;
}
-(void)creatProDetailBrandCellUI
{
    self.brandLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.brandLB];
    self.brandLB.font = FontSet(12);
    self.brandLB.text = @"品牌";
    self.brandLB.textColor = SetColorRGBValue(0x878787, 1.0);
    
    
    self.brandNameLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.brandNameLB];
    self.brandNameLB.font = FontSet(13);
    self.brandNameLB.textColor = SetColorRGBValue(0x333333, 1.0);
    
    self.bottomLine = [[UIView alloc]init];
    self.bottomLine.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    [self.contentView addSubview:self.bottomLine];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat brandLBW = [Utils calculateSizeWithText:self.brandLB.text height:44 fontSize:12].width;
    self.brandLB.frame = CGRectMake(12, 0, brandLBW, 44);
    
    self.brandNameLB.frame = CGRectMake(self.brandLB.right+12, 0, ScreenWidth-36-brandLBW, 44);
    
    self.bottomLine.frame = CGRectMake(0, 44, ScreenWidth, 0.5);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProDetailBrandCellIdentifier";
    ProDetailBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProDetailBrandCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}
-(void)setupProDetailBrandCellData:(NSString *)brandName
{
    self.brandNameLB.text = brandName;
}
@end